
#include "webclient.h"
#include "webclient_if.h"

#define GET_HEADER_BUFSZ               1024
#define GET_RESP_BUFSZ                 1024

#define GET_LOCAL_URI                  "http://www.rt-thread.com/service/rt-thread.txt"


/* send HTTP GET request by common request interface, it used to receive longer data */
static int webclient_get_comm(const char *uri)
{
	_webclient_t *pweb = NULL;
    unsigned char *buffer;
    int index, ret = 0;
    int bytes_read, resp_status;
    int content_length = -1;

    buffer = (unsigned char *)malloc(GET_RESP_BUFSZ);
    if (buffer == NULL)
    {
        printf("no memory for receive buffer.\n");
        ret = -2;
        goto __exit;
    }

    /* create webclient session and set header response size */
    pweb = webclient_create(GET_HEADER_BUFSZ);
    if (pweb == NULL)
    {
        ret = -2;
        goto __exit;
    }

    /* send GET request by default header */
    if ((resp_status = webclient_request_get(pweb, uri)) != 200)
    {
        printf("webclient GET request failed, response(%d) error.\n", resp_status);
        ret = -2;
        goto __exit;
    }

    printf("webclient get response data: \n");

    content_length = webclient_get_content_length(pweb);
    if (content_length < 0)
    {
        printf("webclient GET request type is chunked.\n");
        do
        {
            bytes_read = webclient_read(pweb, (void *)buffer, GET_RESP_BUFSZ);
            if (bytes_read <= 0)
            {
                break;
            }

            for (index = 0; index < bytes_read; index++)
            {
                printf("%c", buffer[index]);
            }
        } while (1);

        printf("\n");
    }
    else
    {
        int content_pos = 0;

        do
        {
            bytes_read = webclient_read(pweb, (void *)buffer, 
                    content_length - content_pos > GET_RESP_BUFSZ ?
                            GET_RESP_BUFSZ : content_length - content_pos);
            if (bytes_read <= 0)
            {
                break;
            }

            for (index = 0; index < bytes_read; index++)
            {
                printf("%c", buffer[index]);
            }

            content_pos += bytes_read;
        } while (content_pos < content_length);

        printf("\n");
    }

__exit:
    if (pweb) webclient_delete(pweb);
    if (buffer) free(buffer);

    return ret;
}

#if 0
/* send HTTP GET request by simplify request interface, it used to received shorter data */
static int webclient_get_smpl(const char *uri)
{
    char *response = NULL;
    size_t resp_len = 0;
    int index;

    if (webclient_request(uri, NULL, NULL, 0, (void **)&response, &resp_len) < 0)
    {
        printf("webclient send get request failed.\n");
        return -2;
    }

    printf("webclient send get request by simplify request interface.\n");
    printf("webclient get response data: \n");
    for (index = 0; index < strlen(response); index++)
    {
        printf("%c", response[index]);
    }
    printf("\n");
    
    if (response) free(response);

    return 0;
}
#endif


int webclient_get_demo(int argc, char **argv)
{
    char *uri = NULL;

    if (argc == 1)
    {
		// uri = malloc(strlen(GET_LOCAL_URI) + 1);
		uri = web_strdup(GET_LOCAL_URI);
        if(uri == NULL)
        {
            printf("no memory for create get request uri buffer.\n");
            return -2;
        }

        webclient_get_comm(uri);
    }
    else if (argc == 2)
    {
        if (strcmp(argv[1], "-s") == 0)
        {
            uri = web_strdup(GET_LOCAL_URI);
            if(uri == NULL)
            {
                printf("no memory for create get request uri buffer.\n");
                return -2;
            }

            // webclient_get_smpl(uri);
        }
        else
        {
			uri = web_strdup(argv[1]);
            if(uri == NULL)
            {
                printf("no memory for create get request uri buffer.\n");
                return -2;
            }
            webclient_get_comm(uri);
        }
    }
    else if(argc == 3 && strcmp(argv[1], "-s") == 0)
    {
		uri = web_strdup(argv[2]);
        if(uri == NULL)
        {
            printf("no memory for create get request uri buffer.\n");
            return -2;
        }

        // webclient_get_smpl(uri);
    }
    else
    {
        printf("web_get_test [URI]     - webclient GET request test.\n");
        printf("web_get_test -s [URI]  - webclient simplify GET request test.\n");
        return -1;
    }

    if (uri) free(uri);
    
    return 0;
}

#include "finsh.h"

MSH_CMD_EXPORT(webclient_get_demo, webclient_get_demo);





